package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.Reservation;
import com.zzyl.mapper.ReservationMapper;
import com.zzyl.service.ReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.CountVo;
import com.zzyl.vo.ReservationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

@Service
public class ReservationServiceImpl implements ReservationService {
    @Autowired
    private ReservationMapper reservationMapper;


    @Override
    public Integer getCancelledCount(Long userId) {
        //获取当前时间
        LocalDateTime start = LocalDate.now().atStartOfDay();
        LocalDateTime end = LocalDate.now().atTime(23, 59, 59);
        Integer count = reservationMapper.getCancelledCount(userId,start,end);
        return count;
    }

    @Override
    public void insertReservation(ReservationDto dto) {
        Reservation reservation = BeanUtil.toBean(dto, Reservation.class);
        reservation.setStatus(0);
        //获取当前线程的userid
        Long userId = UserThreadLocal.getUserId();
        //设置为创建人
        reservation.setCreateBy(userId);
        reservationMapper.insertReservation(reservation);
    }

    @Override
    public List<CountVo> countByTime(Long time) {
        //获取前端传来的时间戳
        LocalDateTime start = Instant.ofEpochMilli(time).atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime().plusHours(8);
        LocalDateTime end = start.plusHours(10);
        List<CountVo> countVos = reservationMapper.countByTime(start,end);
        //获取每个时间段剩余预约次数
        List<CountVo> result = new ArrayList<>();
        //循环判断每半个小时的预约次数
        while (start.isBefore(end) || start.isEqual(end)){
            CountVo countVo = new CountVo(start, 6);
            for (CountVo vo : countVos) {
                if (vo.getTime().equals(start)){
                    countVo.setCount(6-vo.getCount());
                    break;
                }
            }
            result.add(countVo);
            start = start.plusMinutes(30);
        }
        return result;
    }

    @Override
    public PageResponse<ReservationVo> selectByPage(Integer pageNum, Integer pageSize, Integer status) {
        //使用分页工具
        PageHelper.startPage(pageNum,pageSize);
        //查询预约
        List<Reservation> reservations =reservationMapper.selectByPage(status);
        //强转为Page类型
        Page page = (Page) reservations;
        //转为ReservationVo类型
        PageResponse<ReservationVo> response = PageResponse.of(page, ReservationVo.class);
        return response;
    }

    @Override
    public void cancelReservation(Integer id) {
        reservationMapper.cancelReservation(id);
    }

    @Override
    public void overDue() {
        //获取当前时间
        LocalDateTime time = LocalDateTime.now();
        reservationMapper.overDue(time);
    }
}
